/*
 * Smith.cpp
 *
 *  Created on: 31-08-2013
 *      Author: Krzychu
 */

#include "Smith.h"

#include "Config.h"

#include <stdio.h>
#include <string.h>

#define MAX(a, b) (a > b) ? a : b

Smith::Smith()
{

}

Smith::~Smith()
{

}

std::string Smith::getName()
{
	return "Smith";
}

searchResult Smith::execute(std::string text, std::string pattern)
{
	searchResult result;
	result.iterationsTaken = 0;

	int textLength = text.length();
	int patternLength = pattern.length();

	const char* textChar = text.c_str();
	const char* patternChar = pattern.c_str();

	/* Preprocessing */
	int* bmBc = pre_bad_character_shift(pattern);
	int* qsBc = preQsBc(pattern);

	/* Searching */
	int j = 0;
	while (j <= textLength - patternLength)
	{
		result.iterationsTaken++;
		if (memcmp(patternChar, textChar + j, patternLength) == 0)
		{
			result.positionsFound.push_back(j);
			if (Config::getInstance()->getFindOneOccurence())
			{
				return result;
			}
		}
		j += MAX(bmBc[text[j + patternLength - 1]], qsBc[text[j + patternLength]]);
	}

	return result;
}
